package edu.hawaii.myisern.action;

import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.xml.bind.JAXBException;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.xml.sax.SAXException;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;


/**
 * This is a super class to test forms for the myisernweb application.
 * 
 * @author Kevin English
 * 
 */
public class TestWeb {

  /** Application Url. (created to appease PMD) */
  protected WebConversation conversation;
  /*
  private static final String TEST_CONFIG = IsernManager.getApplicationDirectory()
      + File.separatorChar + "appconfig.test.xml";
  private static final String BACKUP_CONFIG = IsernManager.getApplicationDirectory()
      + File.separatorChar + "appconfig.bak.xml";
  private static final String LIVE_CONFIG = IsernManager.getApplicationDirectory()
      + File.separatorChar + "appconfig.xml";
      */
  /** Get the test host in build.xml: http://localhost. */
  protected static String testHost = "http://localhost:8080/";//System.getProperty("test.host");
  /** Application Url. */
  protected static String applicationName = "myisern-violet";

  /**
   * Copy over the test xml files and force the application to reload the xml by calling the
   * ReloadActionBean. This is used so that are live data is not corrupted by our tests.
   * 
   * @throws MalformedURLException On an invalid url.
   * @throws IOException On invalid file
   * @throws SAXException On invalid xml
   * @throws JAXBException On invalid xml
   */

  @BeforeClass
  public static void setUp() 
            throws MalformedURLException, IOException, SAXException, JAXBException {
    /*
    File liveConfig = new File(LIVE_CONFIG);
    File tempBackup = new File(BACKUP_CONFIG);
    File testConfig = new File(TEST_CONFIG);
    // Rename file (or directory)
    liveConfig.renameTo(tempBackup);
    testConfig.renameTo(liveConfig);
    */
    reloadApp("test");
  }

  /**
   * Copy the live xml files back and force the application to reload the xml by calling the
   * ReloadActionBean. This is used so that are live data is not corrupted by our tests. 
   * @throws MalformedURLException On an invalid url.
   * @throws IOException On invalid file
   * @throws SAXException On invalid xml
   */
  @AfterClass
  public static void tearDown() throws MalformedURLException, IOException, SAXException {
    /*
    File liveConfig = new File(LIVE_CONFIG);
    File tempBackup = new File(BACKUP_CONFIG);
    File testConfig = new File(TEST_CONFIG);
    // Rename file (or directory)
    liveConfig.renameTo(testConfig);
    tempBackup.renameTo(liveConfig);
    */
    reloadApp("data");
  }

  /**
   * Setup the web conversation and login to the application. Override this if you want to test
   * a page that does not need to be loggged in, for example, TestLoginActionBean overrides this 
   * method.
   * 
   * @throws MalformedURLException On an invalid url.
   * @throws IOException On invalid file
   * @throws SAXException On invalid xml
   */
  @Before
  public void createSession() throws MalformedURLException, IOException, SAXException {
    this.conversation = new WebConversation();

    WebResponse response = this.conversation.getResponse(getAppUrl() + "/login.jsp");
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    loginRequest.setParameter("username", "myisern");
    loginRequest.setParameter("password", "isern2008");
    response = this.conversation.getResponse(loginRequest);
    assertNotNull("the user session should not be null", conversation.getCookieValue("JSESSIONID"));
  }

  /**
   * Get's the base URL for the application.
   * 
   * @return Application Url
   */

  public static String getAppUrl() {
    return testHost + applicationName;
  }

  /**
   * Force the application to reload the xml files for testing.
   * @param key the key to decide which model to reload.
   * @throws MalformedURLException On an invalid url.
   * @throws IOException On invalid file
   * @throws SAXException On invalid xml
   */
  public static void reloadApp(String key) throws MalformedURLException, IOException, SAXException {
    // force application to reload application config and xml
    String url = getAppUrl() + "/Reload.action?key=" + key;
    WebConversation conversation = new WebConversation();
    conversation.getResponse(url);
    //assertEquals("Retrieving login page.", "Reloaded My ISERN", response.getTitle());
  }
}